4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
19 using System
.Collections
;
20 using System
.Reflection
;
21 using System
.Reflection
.Emit
;
23 public sealed class Block
: AST
{
24 private Completion completion
;
25 private ArrayList list
;
27 internal Block(Context context
)
29 this.completion
= new Completion();
30 this.list
= new ArrayList();
33 internal void Append(AST elem
){
37 internal void ComplainAboutAnythingOtherThanClassOrPackage(){
38 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
39 Object elem
= this.list
[i
];
40 if (elem
is Class
|| elem
is Package
|| elem
is Import
) continue;
41 Block b
= elem
as Block
;
42 if (b
!= null && b
.list
.Count
== 0) continue;
43 Expression e
= elem
as Expression
;
44 if (e
!= null && e
.operand
is AssemblyCustomAttributeList
) continue;
45 ((AST
)elem
).context
.HandleError(JSError
.OnlyClassesAndPackagesAllowed
);
50 internal override Object
Evaluate(){
51 this.completion
.Continue
= 0;
52 this.completion
.Exit
= 0;
53 this.completion
.value = null;
54 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
55 AST elem
= (AST
)(this.list
[i
]);
58 val
= elem
.Evaluate();
59 }catch(JScriptException e
){
60 if (e
.context
== null){
61 e
.context
= elem
.context
;
65 Completion c
= (Completion
)val
;
67 this.completion
.value = c
.value;
69 this.completion
.Continue
= c
.Continue
- 1;
73 this.completion
.Exit
= c
.Exit
- 1;
79 return this.completion
;
82 internal void EvaluateStaticVariableInitializers(){
83 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
84 Object elem
= this.list
[i
];
85 VariableDeclaration vard
= elem
as VariableDeclaration
;
86 if (vard
!= null && vard
.field
.IsStatic
&& !vard
.field
.IsLiteral
){
90 StaticInitializer sinit
= elem
as StaticInitializer
;
95 Class cl
= elem
as Class
;
100 Constant cnst
= elem
as Constant
;
101 if (cnst
!= null && cnst
.field
.IsStatic
){
105 Block block
= elem
as Block
;
107 block
.EvaluateStaticVariableInitializers();
111 internal void EvaluateInstanceVariableInitializers(){
112 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
113 Object elem
= this.list
[i
];
114 VariableDeclaration vard
= elem
as VariableDeclaration
;
115 if (vard
!= null && !vard
.field
.IsStatic
&& !vard
.field
.IsLiteral
){
119 Block block
= elem
as Block
;
121 block
.EvaluateInstanceVariableInitializers();
125 internal override bool HasReturn(){
126 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
127 AST elem
= (AST
)(this.list
[i
]);
128 if (elem
.HasReturn())
134 internal void ProcessAssemblyAttributeLists(){
135 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
136 Expression expr
= this.list
[i
] as Expression
;
138 AssemblyCustomAttributeList acl
= expr
.operand
as AssemblyCustomAttributeList
;
139 if (acl
!= null) acl
.Process();
144 internal void MarkSuperOKIfIsFirstStatement(){
145 if (this.list
.Count
> 0 && this.list
[0] is ConstructorCall
)
146 ((ConstructorCall
)this.list
[0]).isOK
= true;
149 internal override AST
PartiallyEvaluate(){
150 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
151 AST elem
= (AST
)(this.list
[i
]);
152 this.list
[i
] = elem
.PartiallyEvaluate();
157 internal Expression
ToExpression(){
158 if (this.list
.Count
== 1 && this.list
[0] is Expression
)
159 return (Expression
)this.list
[0];
164 internal override void TranslateToIL(ILGenerator il
, Type rtype
){
165 //This assumes that rtype == Void.class
166 Label lab
= il
.DefineLabel();
167 compilerGlobals
.BreakLabelStack
.Push(lab
);
168 compilerGlobals
.ContinueLabelStack
.Push(lab
);
169 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
170 AST elem
= (AST
)(this.list
[i
]);
171 elem
.TranslateToIL(il
, Typeob
.Void
);
174 compilerGlobals
.BreakLabelStack
.Pop();
175 compilerGlobals
.ContinueLabelStack
.Pop();
178 internal override void TranslateToILInitializer(ILGenerator il
){
179 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
180 AST elem
= (AST
)(this.list
[i
]);
181 elem
.TranslateToILInitializer(il
);
185 internal void TranslateToILInitOnlyInitializers(ILGenerator il
){
186 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
187 Constant c
= this.list
[i
] as Constant
;
188 if (c
!= null) c
.TranslateToILInitOnlyInitializers(il
);
192 internal void TranslateToILInstanceInitializers(ILGenerator il
){
193 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
194 AST elem
= (AST
)(this.list
[i
]);
195 if (elem
is VariableDeclaration
&& !((VariableDeclaration
)elem
).field
.IsStatic
&& !((VariableDeclaration
)elem
).field
.IsLiteral
){
196 elem
.TranslateToILInitializer(il
);
197 elem
.TranslateToIL(il
, Typeob
.Void
);
198 }else if (elem
is FunctionDeclaration
&& !((FunctionDeclaration
)elem
).func
.isStatic
)
199 elem
.TranslateToILInitializer(il
);
200 else if (elem
is Constant
&& !((Constant
)elem
).field
.IsStatic
)
201 elem
.TranslateToIL(il
, Typeob
.Void
);
202 else if (elem
is Block
)
203 ((Block
)elem
).TranslateToILInstanceInitializers(il
);
207 internal void TranslateToILStaticInitializers(ILGenerator il
){
208 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
209 AST elem
= (AST
)(this.list
[i
]);
210 if (elem
is VariableDeclaration
&& ((VariableDeclaration
)elem
).field
.IsStatic
||
211 elem
is Constant
&& ((Constant
)elem
).field
.IsStatic
){
212 elem
.TranslateToILInitializer(il
);
213 elem
.TranslateToIL(il
, Typeob
.Void
);
214 }else if (elem
is StaticInitializer
)
215 elem
.TranslateToIL(il
, Typeob
.Void
);
216 else if (elem
is FunctionDeclaration
&& ((FunctionDeclaration
)elem
).func
.isStatic
)
217 elem
.TranslateToILInitializer(il
);
218 else if (elem
is Class
)
219 elem
.TranslateToIL(il
, Typeob
.Void
);
220 else if (elem
is Block
)
221 ((Block
)elem
).TranslateToILStaticInitializers(il
);
225 internal override Context
GetFirstExecutableContext(){
226 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
227 AST elem
= (AST
)(this.list
[i
]);
229 if ((ctx
= elem
.GetFirstExecutableContext()) != null)